Ext.define('Holo.controller.WindowsLogin', {
    extend: 'Ext.app.Controller',
	
    views: [
        'user.WindowsLogin'
    ],
    
    stores: [
        'Users',
    ],
    
    
    refs: [
            {
               selector: 'form',
               ref: 'formConnection'
            },
            
            {
               selector: 'windowslogin',
               ref: 'windowsLogin'
            }
         ],



    
    init: function() {
    	 this.control({
        
	        'windowslogin button[action=connect]': {
		    click : this.subitForm		
				
                },    
			 
                'windowslogin': {
		    //afterrender : this.affectKeyMap		
				
                }      
        
        });
    },

    
    /**
    * Call subitForm() when the user tape enter
    * @param {Ext.cmp}
    * @return void
    */
    affectKeyMap : function(cmp, eOpts){
    	
    	var controller = this;	
    	var nav = Ext.create('Ext.util.KeyNav', Ext.getBody(), {
		    "enter" : function(){
		        controller.subitForm();
		    }
		  }); 
    
    },
    
    
    /**
    * Submit the connection form
    * @param {Ext.cmp}
    * @return void
    */
    subitForm : function(button,  e,  eOpts){
	 	var controller = this;
                this.getFormConnection().submit({
				                success: function(form, action) {
				                      
				                      if(controller.setUser(action.result.data[0])){
				                        controller.getWindowsLogin().close();
				                      	
				                      	controller.initApp();
				                      		
				                      }else{
				                      	Ext.Msg.alert(Error.get('ERR006'),Error.get('ERR00'));
				                      };
				                },
				                
				                failure: function(form, action) {
							        switch (action.failureType) {
							            case Ext.form.action.Action.CLIENT_INVALID:
							                Ext.Msg.alert(Error.get('ERR006'), 'Form fields may not be submitted with invalid values');
							                break;
							            case Ext.form.action.Action.CONNECT_FAILURE:
							                Ext.Msg.alert('Failure', 'Ajax communication failed');
							                break;
							            case Ext.form.action.Action.SERVER_INVALID:
							            	console.log('wrong user');
										 break;
							       }									       
							    }
		 });
     },

    
    
    
    /**
    * Return an instance of user 
    * @param {data} user info
    * @return {booleen} user created ?
    */
    setUser : function(userData){
    
        try{
                var currentUserLogin = Ext.create('Holo.model.User', {
                                                                    id:userData.id,
                                                                    name:userData.name,
                                                                    type:userData.type,
                                                                    status: userData.status,
                                                                    email:userData.email,
                                                                    pwd:userData.pwd,
                                                                    activity:userData.activity,
                                                                    phone:userData.phone,
                                                                    web_site_url:userData.web_site_url,
                                                                    adress_text:userData.adress_text,
                                                                    city:userData.city
                                                                });
                 this.getUsersStore().add(currentUserLogin);
                                         
                 return true;
                
                }catch(err){
                        console.log('erreur : controller/WindowsLogin.setUser() =>'+ err.message);
                        return false;
                }

            
    },
    
    
    
    
    /**
    * Initialize the Appp, create the viewport of the app
    * @return void
    */
    initApp : function(){
	 var controller = this;
         
	//Define some usefull global variable 
   	var currentUser= this.getUsersStore().first();
        GlobalData.currentUserId =  currentUser.get('id');
        GlobalData.currentUser = currentUser;
        //**************** Spots ****************
        var userSpots = Ext.create('Holo.store.Spots', {
            filters: {
                property: 'user_id',
                value   :  GlobalData.currentUserId 
            }    
        });
        Ext.data.StoreManager.add('Spots', userSpots);
                
            
        var controller = this;
        userSpots.load({
            callback : function(records, operation, success){
                if(success){
                    controller.getController('MainMenu').setItemsToSelectSpotMenu();
                }
            }   
        });
        
                
        //**************** Trips ****************
        var userTrips = Ext.create('Holo.store.Trips', {
            filters: {
                property: 'user_id',
                value   :  GlobalData.currentUserId 
            }    
        });
        Ext.data.StoreManager.add('Trips', userTrips);
                
            
       
        userTrips.load({
            callback : function(records, operation, success){
                if(success){
                    controller.getController('MainMenu').setItemsToSelectTripMenu();
                }
            }   
        });
        
        
        ////**************** Apps ****************
        var userApps = Ext.create('Holo.store.Apps', {
            filters: {
                property: 'user_id',
                value   :  GlobalData.currentUserId 
            }    
        });
        Ext.data.StoreManager.add('Apps', userApps);
        
        userApps.load({
            callback : function(records, operation, success){
                if(success){
                    controller.getController('MainMenu').setItemsToSelectAppMenu();
                }
            }   
        });
        
        
        Ext.create('Holo.view.Mainstreet', {
            renderTo: Ext.getBody()
        });
    }
});